package com.kinghunt.misp.common.utils;


import com.kinghunt.misp.common.constant.PermissionConstant;
import com.kinghunt.misp.common.dto.userx.PermissionDTO;
import com.kinghunt.misp.dal.model.userx.*;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangtao
 * @date 2019-10-28 23:58.
 */
public class PermissionConverter {
    public static PermissionDTO convertFromRolePermissionDO(RolePermissionDO rolePermissionDO) {
        PermissionDTO PermissionDTO = new PermissionDTO();
        PermissionDTO.setId(rolePermissionDO.getPermissionId());
        PermissionDTO.setCode(rolePermissionDO.getPermissionCode());
        return PermissionDTO;
    }

    public static List<PermissionDTO> convertFromRolePermissionDOs(List<RolePermissionDO> rolePermissionDOs) {
        if (CollectionUtils.isEmpty(rolePermissionDOs)) {
            return Collections.EMPTY_LIST;
        }
        return rolePermissionDOs.stream().map(PermissionConverter::convertFromRolePermissionDO).collect(Collectors.toList());
    }

    public static RolePermissionDO convert2RolePermissionDO(Long roleId, PermissionDO source) {
        RolePermissionDO target = new RolePermissionDO();
        target.setRoleId(roleId);
        target.setPermissionId(source.getId());
        target.setPermissionCode(source.getCode());
        return target;
    }

    public static List<RolePermissionDO> convert2RolePermissionDOs(Long roleId, List<PermissionDO> source) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.EMPTY_LIST;
        }
        return source.stream().map(a -> convert2RolePermissionDO(roleId, a)).collect(Collectors.toList());
    }

    public static UserPermissionDO convert2UserPermissionDO(Long userId, PermissionDO source) {
        UserPermissionDO target = new UserPermissionDO();
        target.setUserId(userId);
        target.setPermissionId(source.getId());
        target.setPermissionCode(source.getCode());
        target.setValue(PermissionConstant.DEFAULT_USER_PERMISSION_VALUE);
        return target;
    }

    public static List<UserPermissionDO> convert2UserPermissionDOs(Long userId, List<PermissionDO> source) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.EMPTY_LIST;
        }
        return source.stream().map(a -> convert2UserPermissionDO(userId, a)).collect(Collectors.toList());
    }


    public static UserPermissionBlacklistDO convert2UserPermissionBlacklistDO(Long userId, PermissionDO source) {
        UserPermissionBlacklistDO target = new UserPermissionBlacklistDO();
        target.setUserId(userId);
        target.setPermissionId(source.getId());
        target.setPermissionCode(source.getCode());
        target.setValue(PermissionConstant.DEFAULT_USER_PERMISSION_VALUE);
        return target;
    }

    public static List<UserPermissionBlacklistDO> convert2UserPermissionBlacklistDOs(Long userId, List<PermissionDO> source) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.EMPTY_LIST;
        }
        return source.stream().map(a -> convert2UserPermissionBlacklistDO(userId, a)).collect(Collectors.toList());
    }
}
